package com.bindapp.model;

import android.app.Activity;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;

import com.bindapp.R;
import com.bindapp.bean.WsxxBean;
import com.bindapp.databinding.ActivityWsxxBinding;
import com.bindapp.http.MNetHttp;
import com.bindapp.listener.HttpListener;
import com.bindapp.util.PreferencesUtil;
import com.bindapp.util.Static;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by apple on 2018/7/21.
 */

public class WsxxModel extends BaseModel {
    private ActivityWsxxBinding binding;
    private String sex="男";

    public WsxxModel(Context context){
        super(context);
        binding= DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.activity_wsxx,null,false);
        binding.setClick(new ViewClick());
    }

    @Override
    public ActivityWsxxBinding getBinding() {
        return binding;
    }

    public class ViewClick{
        public void onClick(View view){
            switch (view.getId()){
                case R.id.boycircle:
                    selectIsBoy(true);
                    break;
                case R.id.boyimg:
                    selectIsBoy(true);
                    break;
                case R.id.girlcircle:
                    selectIsBoy(false);
                    break;
                case R.id.girlimg:
                    selectIsBoy(false);
                    break;
                case R.id.submit:
                    if(TextUtils.isEmpty(binding.nameview.getText())){
                        Static.showToast(context,"姓名不能为空");
                    }else{
                        showPassDialog("正在保存...");
                        Map<String,String> params=new HashMap<String,String>();
                        params.put("PhoneNumber", PreferencesUtil.getInstance(context).getPhonenum());
                        params.put("Name",binding.nameview.getText().toString());
                        params.put("Gender",sex);
                        MNetHttp.getInstance(context).rxPostRequest(Static.WsxxUrl, params, WsxxBean.class, new HttpListener() {
                            @Override
                            public void success(Object obj) {
                                dissPassDialog();
                                if(((WsxxBean)obj).isSuccess()&&((WsxxBean)obj).isBl()){
                                    Static.showToast(context,"提交成功");
                                    ((Activity)context).finish();
                                }else{
                                    Static.showToast(context,((WsxxBean)obj).getMsg());
                                }


                            }

                            @Override
                            public void fail(String msg) {
                                dissPassDialog();
                                Static.showToast(context,msg);
                            }
                        });
                    }

                    break;
            }

        }
    }

    private void selectIsBoy(boolean isflag){
        sex=isflag?"男":"女";
        binding.boycircle.setBackground(isflag?ContextCompat.getDrawable(context,R.drawable.sex_select):ContextCompat.getDrawable(context,R.drawable.sex_unselect));
        binding.girlcircle.setBackground(isflag?ContextCompat.getDrawable(context,R.drawable.sex_unselect):ContextCompat.getDrawable(context,R.drawable.sex_select));
        binding.boyimg.setImageDrawable(isflag?ContextCompat.getDrawable(context,R.drawable.boy_select):ContextCompat.getDrawable(context,R.drawable.boy_unselect));
        binding.girlimg.setImageDrawable(isflag?ContextCompat.getDrawable(context,R.drawable.girl_unselect):ContextCompat.getDrawable(context,R.drawable.girl_select));
    }
}
